﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using Empresa.Sistema.Datos.Entidades;

namespace Empresa.Sistema.Datos
{
    public class Idiomas
    {

        private string ConnectionString = "Data Source=.;Initial Catalog=SistemaBibliotecaV1;Integrated Security=True";
        public void Adicionar(Idioma idi)
        {
            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_InsertarIdioma";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Codigo", idi.Codigo));
                        cmd.Parameters.Add(new SqlParameter("@Descripcion", idi.Descripcion));
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {

            }

        }

        public void Modificar(Idioma idi)
        {
            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_ModificarIdioma";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Id", idi.Id));
                        cmd.Parameters.Add(new SqlParameter("@Codigo", idi.Codigo));
                        cmd.Parameters.Add(new SqlParameter("@Descripcion", idi.Descripcion));
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {

            }

        }

        public void Eliminar(Idioma idi)
        {

            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_EliminarIdioma";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Id", idi.Id));
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException ex)
            {

            }

        }

        public List<Idioma> Listar()
        {
            List<Idioma> lista = new List<Idioma>();

            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_ListarIdioma";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            var idi = new Idioma();
                            idi.Id = rdr.GetInt32(rdr.GetOrdinal("Id"));
                            idi.Codigo = rdr.GetString(rdr.GetOrdinal("Codigo"));
                            idi.Descripcion = rdr.GetString(rdr.GetOrdinal("Descripcion"));

                            lista.Add(idi);
                        }
                    }

                }
            }
            catch (SqlException ex)
            {

            }
            return lista;
        }

        public List<Idioma> ConsultaPorDescripcion(string Descripcion)
        {
            List<Idioma> lista = new List<Idioma>();

            try
            {
                using (var cnx = new SqlConnection(ConnectionString))
                {
                    cnx.Open();
                    string cmdText = "usp_ConsultaIdiomaPorDescripcion";
                    using (var cmd = new SqlCommand(cmdText, cnx))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Descripcion", Descripcion));
                        SqlDataReader rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            var idi = new Idioma();
                            idi.Id = rdr.GetInt32(rdr.GetOrdinal("Id"));
                            idi.Codigo = rdr.GetString(rdr.GetOrdinal("Codigo"));
                            idi.Descripcion = rdr.GetString(rdr.GetOrdinal("Descripcion"));
                            lista.Add(idi);
                        }
                    }
                }
            }
            catch (SqlException ex)
            {

            }
            return lista;
        }

    }
}
